#include <gdk/gdkkeysyms.h>
#include <string.h>
#include "sms.h"

#define INTRODUCTION_LAYOUT_NAME "layout-introduction"

typedef struct _Title{	
	gdouble icon_x;
	gdouble icon_y;
	gdouble text_x;
	gdouble text_y;
	char *font;
	char *colour;
	GooCanvasItem *icon;
	GooCanvasItem *text;
}Title;

typedef struct _Introduction {
	gdouble detail_bg_x;
	gdouble detail_bg_y;
	gdouble detail_photo_x;
	gdouble detail_photo_y;
	GooCanvasItem *bg;
	TextBox *detail_text;
	GooCanvasItem *detail_bg;
	GooCanvasItem *detail_photo;
}Introduction;

typedef struct _Introduction_Layout
{
	GooCanvasItem *bg;
	Title title;
	Introduction introduction;
}Introduction_Layout;


static gboolean introduction_layout_load_bg(xmlNode *a_node, Layout *layout)
{
	Introduction_Layout *introduction_layout = layout->private;
	GdkPixbuf*  pixbuf;
	xmlChar * val;
	char pic[256];

	if(introduction_layout->bg || !a_node)
		return FALSE;

	val = xmlGetProp (a_node, (xmlChar*)"pic");
	if(!val)
		return FALSE;

	sprintf(pic,"%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
	printf("layout->name=%s\n" ,layout->name);
	pixbuf=sms_load_pic(pic, 800, 480);
	if(!pixbuf)
		return FALSE;

	introduction_layout->bg=goo_canvas_image_new (layout->cavas, pixbuf, 0, 0,NULL);
	return TRUE;
}

static gboolean introduction_layout_load_title(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);

	if(!a_node)
		return FALSE;

	Introduction_Layout *introduction_layout = layout->private;
	GdkPixbuf*  pixbuf;
	xmlChar *val;
	char pic[256];

	introduction_layout->title.icon_x = sms_get_xml_double (a_node, (xmlChar*)"icon_x", 0);
	introduction_layout->title.icon_y = sms_get_xml_double (a_node, (xmlChar*)"icon_y", 0);
	introduction_layout->title.text_x = sms_get_xml_double (a_node, (xmlChar*)"text_x", 0);
	introduction_layout->title.text_y = sms_get_xml_double (a_node, (xmlChar*)"text_y", 0);

	val = xmlGetProp (a_node, (xmlChar*)"colour");


	if(!val) {
		//colour = "black";
		introduction_layout->title.colour = "black";
	} else {
		introduction_layout->title.colour = val;
	}

	val = xmlGetProp (a_node, (xmlChar*)"icon");
	if(!val)
	   	return FALSE;
	sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);

	pixbuf = gdk_pixbuf_new_from_file(pic, NULL);
	if(!pixbuf)
	  	return FALSE;
	introduction_layout->title.icon = goo_canvas_image_new (layout->cavas, pixbuf, introduction_layout->title.icon_x,
	    		                                                           introduction_layout->title.icon_y, NULL);
	g_object_unref (G_OBJECT (pixbuf));

	val = xmlGetProp (a_node, (xmlChar*)"font");
	if(!val) {
		//font = "Sans 20";
		introduction_layout->title.font = "Sans 20";
	} else {
		introduction_layout->title.font = val;
	}

	val = xmlGetProp (a_node, (xmlChar*)"text");

	introduction_layout->title.text = goo_canvas_text_new(layout->cavas, val, introduction_layout->title.text_x,
			                                                                  introduction_layout->title.text_y, 120, GTK_ANCHOR_NW,
			                                                                  "font", introduction_layout->title.font,//introduction_layout->title.font,
			                                                                  "fill-color", introduction_layout->title.colour,
			                                                                  "ellipsize", PANGO_ELLIPSIZE_END,
			                                                                  "alignment", PANGO_ALIGN_LEFT, NULL);

	return TRUE;
}

static gboolean introduction_layout_load_detail(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);

	if(!a_node)
		return FALSE;

	Introduction_Layout *introduction_layout = layout->private;
	GdkPixbuf*  pixbuf;
	xmlNode *cur_node = NULL;
	xmlChar *val;
	SMS *sms = layout->sms;
	char pic[256];

	gdouble text_cavas_x, text_cavas_y, text_cavas_h, text_cavas_w, text_w;
	int bg_rgb, font_rgb;

    introduction_layout->introduction.detail_bg_x = sms_get_xml_double (a_node, (xmlChar*)"x", 0);
    introduction_layout->introduction.detail_bg_y = sms_get_xml_double (a_node, (xmlChar*)"y", 0);
    val = xmlGetProp (a_node, (xmlChar*)"bg");

    if(!val)
    	return FALSE;
    sprintf(pic, "%s/%s/layout/%s/%s", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name, val);
    printf("pic: %s\n", pic);
    pixbuf = gdk_pixbuf_new_from_file(pic, NULL);
    if(!pixbuf)
    	return FALSE;
    introduction_layout->introduction.detail_bg =
    		               goo_canvas_image_new (layout->cavas, pixbuf, introduction_layout->introduction.detail_bg_x,
    		                                                            introduction_layout->introduction.detail_bg_y, NULL);

    for (cur_node = a_node->children; cur_node; cur_node = cur_node->next) {
    	if (cur_node->type == XML_ELEMENT_NODE) {
    		if(strcmp(cur_node->name, "photo")==0) {
    			introduction_layout->introduction.detail_photo_x = sms_get_xml_double (cur_node, (xmlChar*)"x", 0);
    			introduction_layout->introduction.detail_photo_y = sms_get_xml_double (cur_node, (xmlChar*)"y", 0);
    			val = xmlGetProp (cur_node,(xmlChar*) "pic");
    			if(!val)
    			    return FALSE;
    			sprintf(pic,"%s/%s/layout/%s/%s",SMS_RESOURCE_DIR,"default"/*theme*/,layout->name,val);
    			printf("pic: %s\n", pic);
    			pixbuf= gdk_pixbuf_new_from_file(pic, NULL);
    			if(!pixbuf)
    			    return FALSE;
    			introduction_layout->introduction.detail_photo =
    					goo_canvas_image_new (layout->cavas, pixbuf, introduction_layout->introduction.detail_photo_x,
    					    		                                 introduction_layout->introduction.detail_photo_y, NULL);
    		} else if (strcmp(cur_node->name,"text")==0) {
    			text_cavas_x = sms_get_xml_double (cur_node, (xmlChar*)"cavas_x", 0);
    			text_cavas_y = sms_get_xml_double (cur_node, (xmlChar*)"cavas_y", 0);
    			text_cavas_w = sms_get_xml_double (cur_node, (xmlChar*)"cavas_w", 0);
    			text_cavas_h = sms_get_xml_double (cur_node, (xmlChar*)"cavas_h", 0);
    			text_w = sms_get_xml_double (cur_node, (xmlChar*)"text_w", 0);
    			bg_rgb = sms_get_xml_int (cur_node, (xmlChar*)"bg_rgb", -1);
    			font_rgb = sms_get_xml_int (cur_node, (xmlChar*)"font_rgb", -1);

    			printf("text_w: %f\n", text_w);
    			introduction_layout->introduction.detail_text = textbox_new(layout->cavas, text_cavas_x, text_cavas_y,
    					                                                                   text_cavas_w, text_cavas_h,
    					                                                                   text_w, bg_rgb, font_rgb, sms->theme);
    			val = xmlNodeGetContent(cur_node);

    			textbox_set_text(introduction_layout->introduction.detail_text, (gchar *)val);
				printf("text: %s\n", val);
    		}
    	}
    }

	return TRUE;
}

static gboolean introduction_layout_travel_xml(xmlNode *a_node, Layout *layout)
{
	printf("start func %s\n" ,__FUNCTION__);
	Introduction_Layout *introduction_layout = layout->private;
	xmlChar *val;
	char pic[256];
	xmlNode *cur_node = NULL;
	xmlNode *bg_node = NULL;
	xmlNode *animate_node = NULL;
	xmlNode *title_node = NULL;
	xmlNode *detail_node = NULL;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			if(strcmp(cur_node->name,"bg")==0)
				bg_node=cur_node;
			else if(strcmp(cur_node->name,"title")==0)
				title_node = cur_node;
			else if(strcmp(cur_node->name,"detail")==0)
				detail_node = cur_node;
			else if(strcmp(cur_node->name,"animates")==0)
				animate_node = cur_node;
		}
	}
	if(!bg_node||!animate_node||!title_node||!detail_node)
		return FALSE;

	introduction_layout_load_bg(bg_node, layout);
	introduction_layout_load_title(title_node, layout);

	introduction_layout_load_detail(detail_node, layout);
	//teacher_style_layout_load_animates (animate_node->children, layout);
	printf("finish func %s\n" ,__FUNCTION__);
	return TRUE;
}

gboolean introduction_layout_load_config(Layout *layout)
{
	gboolean ret=FALSE;
	xmlDoc *doc = NULL;
	xmlNode *root_element = NULL;
	Introduction_Layout *introduction_layout = layout->private;
	char conf[256];

	if (layout->layout_property.xml_path) {
		strcpy(conf, layout->layout_property.xml_path);
	} else {
		sprintf(conf, "%s/%s/layout/%s/layout.xml", SMS_RESOURCE_DIR, "default"/*theme*/, layout->name);
		printf("layout->name=%s\n", layout->name);
	}

	doc = xmlReadFile(conf, NULL, 0);
	if (doc == NULL) {
		fprintf(stderr,"error: could not parse file %s\n", conf);
		return ret;
	}
	root_element = xmlDocGetRootElement(doc);

	if(root_element)
	{
		xmlNode *cur_node = NULL;
		for (cur_node = root_element; cur_node; cur_node = cur_node->next) {
			if (cur_node->type == XML_ELEMENT_NODE) {
				ret=TRUE;
				break;
			}
		}
		if(ret)
			ret = introduction_layout_travel_xml(cur_node->children, layout);
	}

	xmlFreeDoc(doc);
	xmlCleanupParser();
	return ret;

}

static gboolean introduction_layout_init(Layout *layout,gpointer data)
{
	printf("call %s\n",__FUNCTION__);

	Introduction_Layout *introduction_layout = (Introduction_Layout *)g_malloc0(sizeof(Introduction_Layout)+1);
	layout->private=(void *)introduction_layout;

	layout->cavas = goo_canvas_group_new(layout->parent, NULL);

	if(!introduction_layout_load_config(layout))
	{
		fprintf(stderr,"%s:load config err\n",__FUNCTION__);
		//return FALSE;//exit(-1);
	}
	//setting_layout_reset_icons(layout);

	return TRUE;
}

static void introduction_layout_free_introduction(Layout *layout)
{
	printf("call %s\n",__FUNCTION__);

	Introduction_Layout *introduction_layout;
	introduction_layout=layout->private;

	if (introduction_layout->introduction.bg)
		goo_canvas_item_remove (introduction_layout->introduction.bg);
	if (introduction_layout->introduction.detail_bg)
		goo_canvas_item_remove (introduction_layout->introduction.detail_bg);
	if (introduction_layout->introduction.detail_photo)
		goo_canvas_item_remove (introduction_layout->introduction.detail_photo);
	if (introduction_layout->introduction.detail_text)
		textbox_destroy (introduction_layout->introduction.detail_text);
}

static gboolean introduction_layout_clear (Layout *layout,gpointer data)
{
	Introduction_Layout *introduction_layout;

	printf("call %s\n",__FUNCTION__);

	introduction_layout=layout->private;

	introduction_layout_free_introduction (layout);
	if (introduction_layout->bg)
		goo_canvas_item_remove (introduction_layout->bg);

	g_free(introduction_layout);
	return TRUE;
}

gboolean introduction_layout_key_press(Layout * layout,GdkEventKey *event) {
	printf("start func %s\n" ,__FUNCTION__);

	SMS *sms = (SMS *)layout->sms;

	Introduction_Layout *introduction_layout = layout->private;

	if(event->keyval==GDK_Up){
		return textbox_translate(introduction_layout->introduction.detail_text, TRANSLATE_UP);
	}else if(event->keyval==GDK_Down){
		return textbox_translate(introduction_layout->introduction.detail_text, TRANSLATE_DOWN);
	}else if (event->keyval==GDK_a) {
		//Promptbar *promptbar = sms->items[LAYOUT_ITEM_PROMPTBAR]->private;
		//sms_hide_item(layout->sms, LAYOUT_ITEM_PROMPTBAR);
        if (sms->items[LAYOUT_ITEM_PROMPTBAR]->visable) {
        	layout_hide (sms->items[LAYOUT_ITEM_PROMPTBAR]);
        } else {
        	layout_show (sms->items[LAYOUT_ITEM_PROMPTBAR]);
        }
	}else if(event->keyval==GDK_Escape){
		sms_change_win_to(layout->sms,LAYOUT_WIN_MAIN);
		return TRUE;
	}

	return FALSE;
}

static gboolean introduction_layout_show(Layout *layout,gpointer data) {
	printf("start func %s\n" ,__FUNCTION__);
	//Introduction_Layout *introduction_layout = layout->private;

	return TRUE;
}

static gboolean introduction_layout_hide(Layout *layout,gpointer data) {
	printf("start func %s\n" ,__FUNCTION__);
	//Introduction_Layout *introduction_layout = layout->private;
	layout->inited = FALSE;
	return introduction_layout_clear(layout, NULL);
}

Layout *introduction_layout_new(GooCanvasItem * parent,gpointer sms){
	printf("call %s\n",__FUNCTION__);
	Layout *layout=layout_new(parent, LAYOUT_TYPE_WIN, LAYOUT_WIN_INTRODUCTION, INTRODUCTION_LAYOUT_NAME, sms);
	layout_set_func(layout,"init", introduction_layout_init);
	layout_set_func(layout,"clear", introduction_layout_clear);
	layout_set_func(layout,"key-press", introduction_layout_key_press);
	layout_set_func(layout,"show", introduction_layout_show);
	layout_set_func(layout,"hide", introduction_layout_hide);
	return layout;
}
